//
//  myResumeCellExperience.swift
//  LeDian
//
//  Created by 三秒文化 on 16/3/18.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit

class myResumeCellExperience: LDBaseRecFinderCell {
    
    var experienceLab:UILabel?
    var experienceBgImage:UIImageView?
    
    let rowsViewTag = 555
    
    var jobExpRowView:myResumeCellExpRowViews?
    
    var model = myResumeCellExperienceModel() {
        
        didSet {
            updateFrame()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setDefaultContent()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateFrame() {
        
        let expcount:Int = Int(model.workexperiencecount)!
        
        for i in 0 ..< expcount {
            
            let rowsView = myResumeCellExpRowViews.init(frame: CGRectZero)
            rowsView.tag = rowsViewTag + i
            rowsView.model = myResumeCellExperienceModels_details.yy_modelWithDictionary(model.workexperience[i] as! NSDictionary as [NSObject : AnyObject])!
    
            gapView.addSubview(rowsView)
        }
        
        for i in 0..<expcount {
            
            let views = viewWithTag(rowsViewTag+i) as! myResumeCellExpRowViews
            
            if i < 1 {
                
                views.snp_makeConstraints(closure: { (make) in
                    
                    make.top.equalTo((experienceLab?.snp_bottom)!).offset(LDMargin_V*3)
                    make.left.equalTo(LDMargin_V*3)
                    make.right.equalTo(-LDMargin_V*3)
                    make.height.equalTo(model.rowsheight[i])
                })
                
            } else {
                
                let preView = viewWithTag(rowsViewTag+i-1) as! myResumeCellExpRowViews
                views.snp_makeConstraints(closure: { (make) in
                    make.top.equalTo(preView.snp_bottom)
                    make.left.equalTo(LDMargin_V*3)
                    make.right.equalTo(-LDMargin_V*3)
                    make.height.equalTo(model.rowsheight[i])
                })
            }
        }
    }
    
    func setDefaultContent() {
        
        self.frame = LDFrame.ScreenBounds
        experienceLab = UILabel.setHeadTitleLab("工作经历")
        
        gapView.addSubview(experienceLab!)
        experienceLab!.anchorInCorner(.TopLeft, xPad: Margin_V*3, yPad: Margin_V*4, width: 200, height: 20)
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}


//工作经历总体模型
class myResumeCellExperienceModel: NSObject {
    
    lazy var height:CGFloat = {
        return self.getheight()
    }()
    
    var rowsheight = [CGFloat]()
    var workexperiencecount = ""    // 工作经历数目
    
    var workexperience = [AnyObject]()
    
    func getheight() -> CGFloat{
        
        var H:CGFloat = 0
        
        for item in workexperience {
            
            let models = myResumeCellExperienceModels_details.yy_modelWithDictionary(item as! [NSObject : AnyObject])
            //parse(dict: item as! NSDictionary)
            
//            if models?.jobstrenthscount == 0 {
//                
//                models!.height += 35
//            }
            let rowsH = models!.height + LDMargin_V*2 //+ LDMargin_V//底部间距
            
            rowsheight.append(rowsH)
            H += rowsH
        }
        
        return H + LDMargin_V*4 + 20 + 15 + LDMargin_V
    }
}

//工作经历每个条数模型
class myResumeCellExperienceModels_details: NSObject {
    
    //    var workexperiencecount = ""// 工作经历数目
    
    //    lazy var height:CGFloat = {
    //
    //        return (self.jobDescH + self.samepleLabsH)
    //    }()
    
    var height:CGFloat = LDMargin_V*3*2 + LDMargin_V*3 + LDMargin_V*2//+ LDMargin_V*3 + 20
    
    var jobDescH:CGFloat = 0
    var samepleLabsH:CGFloat = 0
    
    var industryname  = ""     // 行业名称
    var companyname = ""      // 公司名称
    var startdate = ""         // 开始时间
    var enddate = ""          // 结束时间
    var jobtitle = ""         // 职位名称
    var department = ""        // 部门名称
    
    // 工作重点标签数目
    var jobstrenthscount = 0 {
        didSet{
            
            let count = Int(jobstrenthscount)
            let LabsHeight = UILabel.getCellSampleLableH(count, labH: 15, LabMargin_V: LDMargin_H*2)
            
            //            debugPrint("samepleLabsHHeight",height)
            if jobstrenthscount != 0 {
                samepleLabsH = LabsHeight //+ LDMargin_V*3
                height += samepleLabsH
            } else {
                height -= LDMargin_V//*3
            }
        }
    }
    var jobstrenths = [AnyObject]()   // 工作重点标签数组
    
    //工作重点描述
    var jobdesc = "" {
        didSet{
            
            let DescHeight = String.getHeightFormAttr(6, title: jobdesc, size: CGSizeMake(LDFrame.ScreenWidth - LDCellFrame.Margin_H, CGFloat.max), font: LDFont.Font14)
            if jobdesc.characters.count > 0 {
                jobDescH = DescHeight + LDMargin_V
                //            debugPrint("jobDescHHeight",height)
                height += jobDescH
            } else {
                height -= LDMargin_V
            }
        }
    }
}
